package bard.partnermatchingsystem.advice;

import bard.partnermatchingsystem.Common.ErrorCode;
import bard.partnermatchingsystem.domain.R.R;
import bard.partnermatchingsystem.exception.CommonException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class CommonExceptionAdvice {
    @ExceptionHandler(RuntimeException.class)
    public R handleRuntimeException(Exception e) {
        log.info("runtimeException", e);
        return R.error(ErrorCode.SYSTEM_ERROR);
    }

    @ExceptionHandler(CommonException.class)
    public R handleCommonException(CommonException e) {
        if (e.getDescription() != null) {
            log.info("CommonException occurred: Code = {}, Description = {}", e.getCode(), e.getDescription(),e);
            return R.error(e.getCode(), e.getDescription());
        } else {
            log.info("CommonException occurred: Code = {}, Message = {}", e.getCode(), e.getMessage(),e);
            return R.error(e.getCode());
        }


    }
}
